package com.example.logindemo

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.input_login_id.*
import kotlinx.android.synthetic.main.input_password.*
import kotlinx.android.synthetic.main.login_btn.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        userIdEt.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                changeLoginBtn()
            }

            override fun afterTextChanged(p0: Editable?) {
            }

        })
        passwordEt.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                changeLoginBtn()
            }

            override fun afterTextChanged(p0: Editable?) {
            }
        })
        loginIv.setOnClickListener { Toast.makeText(this, "要登录啦！", Toast.LENGTH_LONG).show() }
    }

    private fun changeLoginBtn() {
        if (userIdEt.text.isNotEmpty() && passwordEt.text.isNotEmpty()) {
            loginIv.setImageResource(R.drawable.qq_login2)
        } else {
            loginIv.setImageResource(R.drawable.qq_login1)
        }
    }
}